<?php

namespace ApiBundle\Controller\Admin;

use ApiBundle\Controller\BaseController;
use Symfony\Component\HttpFoundation\Request;

class AccountController extends BaseController
{
    /*
     * 只有超级管理员有权限执行以下接口
     */


    /*
     * 添加/编辑 普通管理员帐号
     */
    public function editAdminAccountAction(Request $request)
    {
        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }

        $conn = $this->get("database_connection");

        $id = $request->get("id");
        $phone = $request->get("phone");
        $nickname = $request->get("nickname");
        $realname = $request->get("realname");
        $email = $request->get("email");
        $admin_role_id = $request->get("admin_role_id");

        if(empty($nickname) || empty($realname) || empty($email)){
            return $this->jsonResponse(1,"参数错误");
        }

        // 判断是否是超级管理员
        $admin = $conn->fetchAssoc("SELECT * FROM admin WHERE id ='". $this->getAdminId() ."'");

        if($admin['admin_role_id'] != "2"){
            return $this->jsonResponse(1,"权限不足");
        }

        $validations = $this->get("helper.validations");
        $strings = $this->get("helper.strings");

        if(!$validations->isEmail($email)){
            return $this->jsonResponse(1,"邮箱格式不正确");
        }

        $data = array(
            "nickname" => $nickname,
            "realname" => $realname,
            "email" => $email
        );


        // 添加
        if($id == "0" || $id == ""){

            if(empty($phone) || empty($admin_role_id)){
                return $this->jsonResponse(1,"参数错误");
            }

            if(!$validations->isMobile($phone)){
                return $this->jsonResponse(1,"手机号格式不正确");
            }
            // 判断手机号是否存在
            if($this->checkPhoneExit($phone)){
                return $this->jsonResponse(1,"手机号已存在");
            }

            // 身份
            $data['admin_role_id'] = $admin_role_id;

            $data['district_id'] = $this->getDistrictId();
            $data['phone'] = $phone;
            $password = substr($phone,-6,6);
            $salt = $strings->generateSalt(6,true);
            $data['salt'] = $salt;
            $data['password'] = $strings->encryptPassword($password,$salt);

            $data['created_at'] = date("Y-m-d H:i:s");

            $conn->insert("admin",$data);
        } else {
            //  编辑
            $data['updated_at'] = date("Y-m-d H:i:s");

            $where = array("id" => $id);
            $conn->update("admin",$data,$where);

        }

        return $this->jsonResponse(0,"操作成功");
    }

    /*
     * 管理员帐号列表
     */
    public function adminAccountListAction(Request $request)
    {
        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }

        $page = $request->get("page","1");

        $rows = $request->get("rows","10");

        $conn = $this->get("database_connection");

        // 判断是否是超级管理员
        $admin = $conn->fetchAssoc("SELECT * FROM admin WHERE id ='". $this->getAdminId() ."'");

        if($admin['admin_role_id'] != "2"){
            return $this->jsonResponse(1,"权限不足");
        }

        $sql = "SELECT a.*, d.name as district_name FROM admin a LEFT JOIN district d ON a.district_id = d.id WHERE a.admin_role_id IN (3,4) AND district_id = '". $this->getDistrictId() ."'";

        // TODO 搜索

        // 关键字
        $search = $request->get("search_keyword");
        if(!empty($search)){

            $sql .= " AND (a.realname LIKE '%". $search ."%' OR a.phone LIKE '%". $search ."%') ";
        }

//        //区域搜索
//        $district_id = $request->get("district_id");
//        if(!empty($district_id)){
//
//            $sql .= " AND a.district_id = '". $district_id ."' ";
//        }


        // 搜索条件下总数
        $search_all_count = count($conn->fetchAll($sql));

        $all_page = ceil($search_all_count / $rows);

        // 分页
        $m = ($page -1) * $rows;

        $sql .= " LIMIT $m,$rows ";

        $data = $conn->fetchAll($sql);

        return $this->jsonResponse(0,"操作成功",array("data"=>$data,"search_all_count"=>$search_all_count,"all_page"=>$all_page));
    }

    /*
     * 管理员帐号 重置密码
     */
    public function resetPasswordAction(Request $request)
    {
        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }

        $conn = $this->get("database_connection");
        $id = $request->get("id");

        $strings = $this->get("helper.strings");


        // 判断是否是超级管理员
        $admin = $conn->fetchAssoc("SELECT * FROM admin WHERE id ='". $this->getAdminId() ."'");

        if($admin['admin_role_id'] != "2"){
            return $this->jsonResponse(1,"权限不足");
        }

        if(empty($id)){
            return $this->jsonResponse(1,"参数错误");
        }

        $user_admin = $conn->fetchAssoc("SELECT * FROM admin WHERE id = '". $id ."' ");

        $password = substr($user_admin['phone'],-6,6);

        $encrypt_password = $strings->encryptPassword($password,$user_admin['salt']);

        $conn->update("admin",array("password" => $encrypt_password),array("id" => $id));

        return $this->jsonResponse(0,"操作成功");


    }

    /*
     * 启用/禁用 管理员帐号
     */
    public function changeAdminAuthAction(Request $request)
    {

        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }

        $id = $request->get("id");
        $status = $request->get("status");
        $conn = $this->get("database_connection");

        // 判断是否是超级管理员
        $admin = $conn->fetchAssoc("SELECT * FROM admin WHERE id ='". $this->getAdminId() ."'");

        if($admin['admin_role_id'] != "2"){
            return $this->jsonResponse(1,"权限不足");
        }

        if(empty($id)){
            return $this->jsonResponse(1,"参数错误");
        }


        $conn->update("admin",array("status"=>$status),array("id" => $id));

        if($status == "0"){

            return $this->jsonResponse(0,"禁用成功");

        } else if($status == "1"){

            return $this->jsonResponse(0,"启用成功");
        } else {

            return $this->jsonResponse(1,"status为空");
        }

    }
}
